package com.yumijinfu.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.yumijinfu.R;
import com.yumijinfu.http.HttpUtils;
import com.yumijinfu.tool.AbActivityManager;
import com.yumijinfu.tool.SharedPreferencesUtils;
import com.yumijinfu.view.SwipeBackLayout;
import com.yumijinfu.view.TitleView;
import com.yumijinfu.view.statusView.ActivityStatusView;


/**
 * 作者：黑哥 on 2016/3/29 09:53
 */
public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
    protected String TAG = this.getClass().getSimpleName();
    protected Context context;
    protected Activity activity;
    //滑动关闭activity
    private SwipeBackLayout layout;
    //对外提供一个title的方法
    protected TitleView titles;

    private boolean isFirst = true;
    protected TitleView titleView;
    //无数据无网络加载中布局
    public ActivityStatusView status;
    //
    protected SharedPreferencesUtils mpreferencesUtils;
//    protected LoadingDialog loadingDialog;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        initSystemBarTint();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        activity = this;
        context = this;
        AbActivityManager.getInstance().addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mpreferencesUtils = SharedPreferencesUtils.getInstance();
//        loadingDialog=new LoadingDialog(context, R.style.LoadingDialogStyle);

        HttpUtils.init(this);
        //创建状态布局
//        status=new ActivityStatusView(context,activity);
//        status.setTitleView(titleView);
//        titleView = (TitleView) this.findViewById(R.id.title);
//        initSwipeBack();
//        StatusBarUtil.StatusBarLightMode(this);
        initTitle();
        initView();
        setData();
    }

    protected abstract void initTitle();

    protected abstract void initView();

    protected abstract void setData();
    //事件监听
    protected abstract void widgetClick(View view);


    /**
     * 初始化滑动返回
     */


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(fastClick()){
            widgetClick(v);
        }
    }

    /**
     *
     * @return 控制短时间内重复点击事件－－－－看个人爱好而定
     */
    private long lastClick =0;
    private boolean fastClick(){
        if(System.currentTimeMillis()-lastClick<=500){
            return false;
        }

        lastClick=System.currentTimeMillis();
        return true;

    }

    /**
     *
     * 设置状态栏颜色
     */





    protected void initSystemBarTint() {
        Window window = getWindow();
        if (translucentStatusBar()) {
            // 设置状态栏全透明
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
            return;
        }
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0以上使用原生方法
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(setStatusBarColor());
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4-5.0使用三方工具类
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintColor(setStatusBarColor());
        }
    }


    /** 获取主题色 */
    public int getColorPrimary() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        return typedValue.data;
    }

    /** 获取深主题色 */
    public int getDarkColorPrimary() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
        return typedValue.data;
    }

    /** 子类可以重写改变状态栏颜色 */
    protected int setStatusBarColor() {
        return getColorPrimary();
    }

    /** 子类可以重写决定是否使用透明状态栏 */
    protected boolean translucentStatusBar() {
        return false;
    }

    /** 点击空白处隐藏软键盘*/

    protected  InputMethodManager hiddenKeybord(){

        InputMethodManager  manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        return manager;
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        // TODO Auto-generated method stub
//        if(event.getAction() == MotionEvent.ACTION_DOWN){
//            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
//                hiddenKeybord().hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//            }
//        }
//        return super.onTouchEvent(event);
//    }
}
